package com.pcjavanet.generate;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;





public class ServiceClassCreateTemplate extends CreateClassTemplate {
	private static final String BLANK_TWO_SPACE= "  ";


	@Override
	public String createFieldsDefineStr() {
		StringBuffer buf = new StringBuffer();
		buf.append("\r\n").append("  @Autowired\r\n");
		return buf.toString();
	}

	@Override
	public String createActionMethodByName(String methodName) {
		return "";
	}

	@Override
	public String createGetSetMethodStr() {
		return "";
	}

	@Override
	public String createImportStr() {
		StringBuffer buf = new StringBuffer();
		String beanName = super.getCls().getName();
		buf.append( "import java.util.List;\r\n");
		Field[] fs = ClassUtil.getAllRefFields(getFs());
		if ( fs.length > 0 ){
			buf.append("import java.util.ArrayList;\r\n");
			buf.append("import java.util.HashMap;  \r\n");
			buf.append("import java.util.Iterator; \r\n");
			buf.append("import java.util.Map;      \r\n");

		}else {
			buf.append("\r\n");
		}
		
		if ( fs.length > 0 ){
			for( int i = 0 ; i < fs.length ; i++ ){
	 			String fieldName = fs[i].getName();
				String refbeanName =  ClassUtil.getBeanNameFromRefFieldName( fieldName );
				buf.append("import "+this.getBasepackagepath()+".model."+refbeanName+";\r\n");
				buf.append("import "+this.getBasepackagepath()+".model."+refbeanName+"Example;\r\n");
			}
		}
		buf.append("import "+beanName+"; \r\n");
		buf.append("import "+this.getBasepackagepath()+".model.PageBean;\r\n");
		buf.append("import "+this.getBasepackagepath()+".model."+super.getCls().getSimpleName()+"Example;\r\n");
		if ( getImportContent().size() > 0) {
			for(String importContent :  getImportContent() )
				buf.append(  importContent ).append("\r\n");
		}
		if ( fs.length > 0 ){
			for( int i = 0 ; i < fs.length ; i++ ){
	 			String fieldName = fs[i].getName();
				String refbeanName =  ClassUtil.getBeanNameFromRefFieldName( fieldName );
				buf.append("import "+getBasepackagepath()+".persistence."+refbeanName+"Mapper;\r\n");
			}
		}
		buf.append("import "+getBasepackagepath()+".persistence."+super.getCls().getSimpleName()+"Mapper;\r\n");
		buf.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		buf.append("import org.springframework.stereotype.Service;\r\n");
		buf.append("import org.springframework.transaction.annotation.Transactional;\r\n");
		return buf.toString();                                                             
	}

	@Override
	public String createBodyBegin() {
		StringBuffer buf = new StringBuffer();
		buf.append("\r\n").append("@Service\r\n");
		buf.append("public class "+super.getCls().getSimpleName()+"Service {\r\n");
		return buf.toString();
	}

	@Override
	public String createInsertMethod() {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("\r\n").append(BLANK_TWO_SPACE).append("public int  ").append("insert")/*.append(getCls().getSimpleName())*/.append("( "+ simpleName +" "+ClassUtil.getLowerStartName(super.getCls().getSimpleName())+" ) throws Exception{\r\n" );
		buf.append("	return ").append(name).append("Mapper.insert ("+ClassUtil.getLowerStartName( simpleName )+");\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	@Override
	public String createQueryMethod() {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("\r\n").append(BLANK_TWO_SPACE).append("public "+getCls().getSimpleName() +"  ").append("selectByPrimaryKey").append("( ").append( simpleName ).append("  bean").append(" ) {\r\n" );
		buf.append(" 	int dbid = bean.getDbid();\r\n");
		buf.append("	").append(simpleName).append(" " ).append(name).append(" = ").append(name).append("Mapper.selectByPrimaryKey").append("( dbid );\r\n");
		 
		Field[] fs = ClassUtil.getAllRefFields(getFs());
	 	if ( fs.length > 0 ){
	 		for( int i = 0 ; i < fs.length ; i++ ){
	 			String fieldName = fs[i].getName();
				String beanName =  ClassUtil.getBeanNameFromRefFieldName( fieldName );
				String smallBeanName = beanName.substring(0, 1).toLowerCase()+beanName.substring(1, beanName.length());
				String bigFielName = fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1, fieldName.length());
				if ( beanName.equalsIgnoreCase(  simpleName ) ){
					buf.append("	").append( beanName ).append( " ").append( smallBeanName ).append(i).append(" = ").append(smallBeanName).append("Mapper.selectByPrimaryKey").append("( ").append( name).append(".get").append(bigFielName).append("()").append(" );\r\n");
					buf.append("	").append( name ).append(".set").append(beanName).append("("+ smallBeanName+i+ ")").append(" ;\r\n");
				} else {
					buf.append("	").append( beanName ).append( " ").append( smallBeanName ).append(" = ").append(smallBeanName).append("Mapper.selectByPrimaryKey").append("( ").append( name).append(".get").append(bigFielName).append("()").append(" );\r\n");
					buf.append("	").append( name ).append(".set").append(beanName).append("("+ smallBeanName+ ")").append(" ;\r\n");
				}
	 		}
	 	}
		
		
		buf.append("	return ").append(name).append(" ;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	@Override
	public String createUpdateMethod() {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("\r\n").append(BLANK_TWO_SPACE).append("public int  ").append("updateByPrimaryKeySelective").append("( "+ simpleName +" "+ClassUtil.getLowerStartName( simpleName )+" ) throws Exception{\r\n" );
		buf.append("	return ").append(name).append("Mapper.updateByPrimaryKeySelective").append("( "+ClassUtil.getLowerStartName( simpleName )+" );\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	@Override
	public String createDeleteMethod() {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("\r\n").append(BLANK_TWO_SPACE).append("public int  ").append("deleteByPrimaryKey").append("( ").append( simpleName ).append("  bean ) throws Exception{\r\n" );
		buf.append(" 	int dbid = bean.getDbid();\r\n");
		buf.append("	return ").append(name).append("Mapper.deleteByPrimaryKey( dbid );\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	@Override
	public String createListMethod() {
		StringBuffer buf = new StringBuffer();
		buf.append(createSelectByExampleMethod() );
		buf.append(createGetOnePageBeansMethod() );
		buf.append(createtTotalRecordsMethod() );
		buf.append(createCountByExample() );
		buf.append(createSelectByExamplee() );
		return buf.toString();
	}

	
	public String createGetOnePageBeansMethod(){
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("\r\n").append(BLANK_TWO_SPACE).append("public List<").append(simpleName).append(">  ").append("getOnePageBeans").append("( QryCondition condition , PageBean pageBean ) throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append("  ").append( getCls().getSimpleName()).append("Example example = new ").append( getCls().getSimpleName()).append("Example();\r\n");
		buf.append(BLANK_TWO_SPACE).append("  ServiceUtil.dealCriteria( condition,  example.createCriteria() );\r\n");
		buf.append("	").append("List<").append(simpleName).append("> ").append("ls").append("=").append(name).append("Mapper.getOnePageBeans( example , pageBean );\r\n");
		buf.append("//begin\r\n");
		buf.append("	").append("if ( ls.size() > 0 ) {\r\n");
		Field[] fs = ClassUtil.getAllRefFields(getFs());
		if ( fs.length > 0 ){
			for( int i = 0 ; i < fs.length ; i++ ){
				String fieldName = fs[i].getName();
				String refbeanName =  ClassUtil.getBeanNameFromRefFieldName( fieldName );
				String smallBeanName = refbeanName.substring(0, 1).toLowerCase()+refbeanName.substring(1, refbeanName.length());
				String bigFielName = fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1, fieldName.length()); 
				String smallFielName = fieldName.substring(0, 1).toLowerCase()+fieldName.substring(1, fieldName.length()); 
				buf.append(BLANK_TWO_SPACE).append("		Map<Integer,Integer> "+smallBeanName+"Idmap = new HashMap<Integer,Integer>();\r\n");
				buf.append(BLANK_TWO_SPACE).append("		for("+simpleName+"  m : ls){\r\n");
			 
				buf.append(BLANK_TWO_SPACE).append("			m.set"+refbeanName+"(new "+refbeanName+" ()) ; \r\n");
				buf.append(BLANK_TWO_SPACE).append("			if ( m.get"+bigFielName+"() != null ) \r\n");
				buf.append(BLANK_TWO_SPACE).append("				").append(smallBeanName+"Idmap.put(m.get"+bigFielName+"(), m.get"+bigFielName+"());\r\n");
				buf.append(BLANK_TWO_SPACE).append("		};\r\n");
			    buf.append(BLANK_TWO_SPACE).append("		List<Integer> "+smallFielName+"s = new ArrayList<Integer>();\r\n");
			    buf.append(BLANK_TWO_SPACE).append("		Iterator<Integer> "+smallFielName+"it = "+smallBeanName+"Idmap.keySet().iterator(); \r\n");
			    buf.append(BLANK_TWO_SPACE).append("		while ( "+smallFielName+"it.hasNext() ){\r\n");
			    buf.append(BLANK_TWO_SPACE).append("			"+smallFielName+"s.add( "+smallFielName+"it.next() );\r\n");
			    buf.append(BLANK_TWO_SPACE).append("		}\r\n");
			    buf.append(BLANK_TWO_SPACE).append("		"+refbeanName+"Example "+smallBeanName+"Example = new "+refbeanName+"Example();\r\n");
			    buf.append(BLANK_TWO_SPACE).append("		if ( ").append(smallFielName).append("s.size() > 0 ) \r\n");
			    buf.append(BLANK_TWO_SPACE).append("			"+smallBeanName+"Example.createCriteria().andDbidIn( "+smallFielName+"s );\r\n");
			    buf.append(BLANK_TWO_SPACE).append("		List<"+refbeanName+"> "+smallBeanName+"s = "+smallBeanName+"Mapper.selectByExample( "+smallBeanName+"Example);\r\n");
			    buf.append(BLANK_TWO_SPACE).append("		for("+simpleName+" m : ls ){\r\n");
			    buf.append(BLANK_TWO_SPACE).append("			for("+refbeanName+" c : "+smallBeanName+"s ){\r\n");
			    if (fs[i].getType() == Integer.class )
			    	buf.append(BLANK_TWO_SPACE).append("				if ( m.get"+bigFielName+"() !=null && c.getDbid() !=null && m.get"+bigFielName+"().intValue() == c.getDbid().intValue() ) {\r\n");
			    else 
			    	buf.append(BLANK_TWO_SPACE).append("				if ( m.get"+bigFielName+"() == c.getDbid() ) {\r\n");
			    buf.append(BLANK_TWO_SPACE).append("					m.set"+refbeanName+"( c );\r\n");
			    buf.append(BLANK_TWO_SPACE).append("				}\r\n");
			    buf.append(BLANK_TWO_SPACE).append("			}\r\n");
			    buf.append(BLANK_TWO_SPACE).append("		}\r\n");
			}
		}
		buf.append("	").append("}\r\n");
		buf.append("//end\r\n");
		buf.append("	return ").append("ls").append(";\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}
	
	public String createSelectByExampleMethod(){
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("\r\n").append(BLANK_TWO_SPACE).append("public List<").append(simpleName).append(">  ").append("selectByExample").append("( "+simpleName +" "+ name +" ) throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append("  ").append( getCls().getSimpleName()).append("Example example = new ").append( getCls().getSimpleName()).append("Example();\r\n");
		buf.append("	return ").append(name).append("Mapper.selectByExample( example );\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}
	
	public String createCountByExample( ) {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("\r\n").append(BLANK_TWO_SPACE).append("public int countByExample( "+simpleName +" "+ name +" , PageBean pageBean ){\r\n");
		buf.append(BLANK_TWO_SPACE).append("  ").append( getCls().getSimpleName()).append("Example example = new ").append( getCls().getSimpleName()).append("Example();\r\n");
		buf.append("	return ").append(name).append("Mapper.countByExample( example );\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	public String createSelectByExamplee( ) {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append(BLANK_TWO_SPACE).append("public List<"+simpleName+"> selectByExamplePage( "+simpleName +" "+ name +" , PageBean pageBean ){\r\n");
		buf.append(BLANK_TWO_SPACE).append( getCls().getSimpleName()).append("Example example = new ").append( getCls().getSimpleName()).append("Example();\r\n");
		buf.append("	return ").append(name).append("Mapper.selectByExamplePage( example , pageBean );\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}
	
	public String createtTotalRecordsMethod(){
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("\r\n").append(BLANK_TWO_SPACE).append("public int ").append("getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append("  ").append( getCls().getSimpleName()).append("Example example = new ").append( getCls().getSimpleName()).append("Example();\r\n");
		buf.append(BLANK_TWO_SPACE).append("  ServiceUtil.dealCriteria( condition,  example.createCriteria() );\r\n");
		buf.append("	return ").append(name).append("Mapper.getTotalRecords( example , pageBean );\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}
	
	public String createBeanServiceStr(){
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("  private ").append(" ").append( getCls().getSimpleName()).append("Mapper").append(" ").append(name).append("Mapper;\r\n");
		buf.append( createRefBeanServiceStr());
		return buf.toString();
	}
	
	public String createRefBeanServiceStr(){
		StringBuffer buf = new StringBuffer();
	 	Field[] fs = ClassUtil.getAllRefFields(getFs());
	 	if ( fs.length > 0 ){
	 		List<String> beanNames = ClassUtil.getBeanNamesByFields( Arrays.asList(fs));
	 		for( int i = 0 ; i < beanNames.size() ; i++ ){
			String beanName = beanNames.get(i);
			String smallBeanName = beanName.substring(0, 1).toLowerCase()+beanName.substring(1, beanName.length());
			if ( beanName.equalsIgnoreCase( getCls().getSimpleName())) {
				continue ; 
			} else {
			buf.append("").append("  @Autowired\r\n");
			buf.append("  private ").append(" ").append( beanName ).append("Mapper").append(" ").append( smallBeanName ).append("Mapper;\r\n");
			}
		 	}
	 	}
		return buf.toString();
	}

}
